﻿(function () {
    "use strict";

    var ui = WinJS.UI;
    var utils = WinJS.Utilities;

    var storage = Windows.Storage;
    var dtm = Windows.ApplicationModel.DataTransfer.DataTransferManager;
    var item;

    ui.Pages.define("/pages/itemDetail/itemDetail.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            if (!checkInternetConnection()) {
                return;
            }

            item = options && options.item ? Data.resolveItemReference(options.item) : Data.items.getAt(0);
            element.querySelector(".titlearea .pagetitle").textContent = item.group.title;
            element.querySelector("article .item-title").textContent = item.title;
            //element.querySelector("article .item-subtitle").textContent = item.preptime;
            element.querySelector("article .item-image").src = item.backgroundImage;
            element.querySelector("article .item-image").alt = item.shortTitle;

            // Display ingredients list
            //var ingredients = element.querySelector("article .item-ingredients");
            //for (var i = 0; i < item.ingredients.length; i++) {
            //    var ingredient = document.createElement("h2");
            //    ingredient.textContent = item.ingredients[i];
            //    ingredient.className = "ingredient";
            //    ingredients.appendChild(ingredient);
            //}

            // Display cooking directions
            //element.querySelector("article .item-directions").textContent = item.directions;
            //element.querySelector(".content").focus();

            // Register for datarequested events for sharing
            // dtm.getForCurrentView().addEventListener("datarequested", this.onDataRequested);

            // Load merchant page.            
            loadMerchantPage(element, item);
        },

        /*
        // ======================== SHARE PROCESSING - BEGIN ===========================//
        onDataRequested: function (e) {
            var request = e.request;
            request.data.properties.title = item.title;
            // request.data.properties.description = "Recipe ingredients and directions";

            // Share recipe text
            // var recipe = "\r\nINGREDIENTS\r\n" + item.ingredients.join("\r\n");
            // recipe += ("\r\n\r\nDIRECTIONS\r\n" + item.directions);
            // request.data.setText(recipe);

            // Share recipe image
            var uri = item.backgroundImage;
            if (item.backgroundImage.indexOf("http://") != 0)
                uri = "ms-appx:///" + uri;

            uri = new Windows.Foundation.Uri(uri);
            var reference = storage.Streams.RandomAccessStreamReference.createFromUri(uri);
            request.data.properties.thumbnail = reference;
            request.data.setBitmap(reference);
        },

        unload: function () {
            WinJS.Navigation.removeEventListener("datarequested", this.onDataRequested);
        }
        // ======================== SHARE PROCESSING - END ===========================//
        */
    });

    function loadMerchantPage(element, item) { // http://www.tipons.com/view_merchants/CURRYSHIKA.html
        // Get merchant ID.
        var merchantId = item.id;

        // Prepare merchant URI.
        var uri = String.format(MERCHANT_PAGE, merchantId);

        WinJS.xhr({ url: uri }).then(function (xhr) {
            if (xhr.status == 200) {
                // Parse xhr to JSON.
                var responseText = xhr.responseText.trim();
                var response = xhr.response;

            } else {
                $("#merchantDescription").text("Merchant information is pending approval.").show();
            }

            return xhr;
        }).done(
            function onComplete(result) {
                // Load content in iframe.
                $("#merchantFrame").attr("src", uri).show();
            },

            function onError(error) {
                $("#merchantFrame").hide();
                $("#merchantDescription").text("Merchant information is pending approval.").show();
            }
        );
    }

})();

//function autoResize(id) {
//    var newheight;
//    var newwidth;

//    if (document.getElementById) {
//        newheight = document.getElementById(id).contentWindow.document.body.scrollHeight;
//        newwidth = document.getElementById(id).contentWindow.document.body.scrollWidth;
//    }

//    document.getElementById(id).height = (newheight) + "px";
//    document.getElementById(id).width = (newwidth) + "px";
//}